package logic

import (
	"context"
	"models"

	"shop_srv/internal/svc"
	"shop_srv/shop_srv"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *shop_srv.GoodeListRequest) (*shop_srv.GoodsListResponse, error) {
	// todo: add your logic here and delete this line
	var goodses []models.Goods
	if err := models.DB.Find(&goodses).Error; err != nil {
		return nil, err
	}
	var Goods []*shop_srv.Goods
	for _, goods := range goodses {
		gs := shop_srv.Goods{
			Name:      goods.GoodsName,
			Price:     goods.GoodsPrice,
			Stock:     goods.GoodsStock,
			Max:       goods.GoodsNumMax,
			UserId:    goods.UserId,
			CreatedAt: goods.CreatedAt.String(),
		}
		Goods = append(Goods, &gs)
	}
	return &shop_srv.GoodsListResponse{
		Goods: Goods,
	}, nil
}
